package com.cooldatasoft.web.cms.personProperty;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.cooldatasoft.app.WOCApplication;
import com.cooldatasoft.app.WOCSession;
import com.cooldatasoft.base.BaseViewPage;
import com.cooldatasoft.data.PersonProperty;

public class PersonPropertyViewPage extends BaseViewPage{

	
	private FeedbackPanel feedbackPanel;
	
	public PersonPropertyViewPage(Integer entityId) {
		
		MarkupContainer container= new WebMarkupContainer("container");		
		MarkupContainer header = new WebMarkupContainer("header");
		ExternalLink companyWebsiteLink = new ExternalLink("companyWebsiteLink","http://www.cooldatasoft.com","Company Name");
		companyWebsiteLink.add(new AttributeModifier("target",true,new Model("_blank")));
		header.add(companyWebsiteLink);
		container.add(header);
		
		
		MarkupContainer wrapper = new WebMarkupContainer("wrapper");
		MarkupContainer personalInfoDiv = new WebMarkupContainer("personalInfoDiv");
		PersonPropertyForm personalInfoForm = new PersonPropertyForm("personalInfoForm",entityId);
		personalInfoDiv.add(personalInfoForm);
		wrapper.add(personalInfoDiv);
		
		container.add(wrapper);
		
		add(container);
	}

	
	class PersonPropertyForm extends Form{

		private PersonProperty personProperty;
		
		public PersonPropertyForm(String id,Integer entityId) {
			super(id);
			if(entityId!=null){
				personProperty = WOCApplication.get().getPersonServiceProxy().getPersonPropertyById(entityId);
			}
			Label idLabel = new Label("idLabel","Id");
			Label labelLabel = new Label("labelLabel","Label");
			Label publicLabel = new Label("publicLabel","Is Public");
			Label comboLabel = new Label("comboLabel","Is Combo");
			Label dataTypeLabel = new Label("dataTypeLabel"," Data type");
			Label sequenceLabel = new Label("sequenceLabel"," Sequence");
			
			add(idLabel);
			add(labelLabel);
			add(publicLabel);
			add(comboLabel);
			add(dataTypeLabel);
			add(sequenceLabel);
			
			Label idValue = new Label("idValue",new PropertyModel(this,"personProperty.id"));
			TextField labelTextField = new TextField("labelTextField",new PropertyModel(this,"personProperty.label"));
			CheckBox publicCheckBox = new CheckBox("publicCheckBox",new PropertyModel(this,"personProperty.public"));
			CheckBox comboCheckBox = new CheckBox("comboCheckBox",new PropertyModel(this,"personProperty.combo"));
			TextField sequenceTextField = new TextField("sequenceTextField",new PropertyModel(this,"personProperty.sequence"));
			
			List<String> dataTypeList = new ArrayList<String>();
			dataTypeList.add("STRING");
			DropDownChoice dataTypeDropDownChoice = 
				new DropDownChoice("dataTypeDropDownChoice",new PropertyModel(this,"personProperty.dataType"),dataTypeList);
			
			add(idValue);
			add(labelTextField);
			add(publicCheckBox);
			add(comboCheckBox);
			add(dataTypeDropDownChoice);
			add(sequenceTextField);
			
			Button personalInfoSaveButton = new Button("personalInfoSaveButton");
			add(personalInfoSaveButton);			
		}

		@Override
		protected void onSubmit() {
			personProperty.setCreateUserId(WOCSession.get().getUser().getId());
			personProperty.setUpdateUserId(WOCSession.get().getUser().getId());
			personProperty.setUpdateDate(new Date());
			WOCApplication.get().getPersonServiceProxy().saveOrUpdate(personProperty);
			setResponsePage(PersonPropertyListPage.class);
		}

		public PersonProperty getPersonProperty() {
			return personProperty;
		}

		public void setPersonProperty(PersonProperty personProperty) {
			this.personProperty = personProperty;
		}
	}
}
